package com.example.demo.controller;

import com.example.demo.common.ApiCodeConstant;
import com.example.demo.common.ApiResult;
import com.example.demo.common.Constant;
import com.example.demo.common.MyException;
import com.example.demo.dto.UserDto;
import com.example.demo.entity.User;
import com.example.demo.query.UserQuery;
import com.example.demo.service.UserService;
import com.example.demo.utils.Base64Util;
import com.example.demo.utils.RedisUtil;
import com.example.demo.utils.TokenUtil;
import com.example.demo.vo.LoginUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

 /*   @PostMapping("/login")
    public User login(@RequestBody User user){
//        System.out.println("username="+username+",password="+password);
        System.out.println("username="+user.getUsername()+",password="+user.getPassword());
        return userService.login(user.getUsername(),user.getPassword());
    }*/

   /* @PostMapping("/api/login")
    public ApiResult<Map<String,Object>> apiLogin(@RequestBody User fromUser,@RequestHeader(value = "client_type",required = false) String clientType) throws UnsupportedEncodingException {
//        System.out.println("username="+username+",password="+password);
        System.out.println("username="+fromUser.getUsername()+",password="+fromUser.getPassword());
        User user = userService.login(fromUser.getUsername(),fromUser.getPassword());
        if(user!=null){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("user",user);
            //如果是Android app发送的请求
            String tokenKey = "token:"+user.getId();

            if(Constant.CLIENT_TYPE_ANDROID.equals(clientType) || Constant.CLIENT_TYPE_IOS.equals(clientType) || Constant.CLIENT_TYPE_WECHAT.equals(clientType)){
                //redisUtil.hset(tokenKey,clientType,TokenUtil.generateTokenValue(clientType,user.getId()),TOKEN_EXPIRE_TIME);
                Map<String, Object> map = new HashMap();
                String tokenValue = TokenUtil.generateTokenValue(clientType,user.getId());
                map.put(clientType, tokenValue);
                redisUtil.hmset(tokenKey,map,Constant.TOKEN_EXPIRE_TIME);
                dataMap.put("token", Base64Util.encode(user.getId()+"-"+tokenValue));
            } else {
                //redisUtil.hset(tokenKey,Constant.CLIENT_TYPE_WEB,TokenUtil.generateTokenValue(Constant.CLIENT_TYPE_WEB,user.getId()),Constant.TOKEN_EXPIRE_TIME);
                Map<String, Object> map = new HashMap();
                String tokenValue = TokenUtil.generateTokenValue(Constant.CLIENT_TYPE_WEB,user.getId());
                map.put(Constant.CLIENT_TYPE_WEB, tokenValue);
                redisUtil.hmset(tokenKey,map,Constant.TOKEN_EXPIRE_TIME);
                //将userid和tokenValue使用"-"拼接在一起,然后采用Base64混淆之后返回给前端
                dataMap.put("token",Base64Util.encode(user.getId()+"-"+tokenValue));
            }
            return ApiResult.success(dataMap);
        } else{
            return ApiResult.failure(ApiCodeConstant.RESULT_LOGIN_FAIL,"用户名或密码错误");
        }
    }*/

    @PostMapping("/api/login")
    public ApiResult<LoginUserVo> apiLogin(@RequestBody @Valid UserQuery fromUser, @RequestHeader(value = "client_type",required = false,defaultValue = Constant.CLIENT_TYPE_WEB) String clientType) throws UnsupportedEncodingException {
        System.out.println("username="+fromUser.getUsername()+",password="+fromUser.getPassword());
    /*    String str= null;
        str.equals("aaa");*/
        UserDto user = userService.login(fromUser.getUsername(),fromUser.getPassword());
        if(user!=null){
            String token = userService.generateToken(user,clientType);
            LoginUserVo loginUserVo = new LoginUserVo();
            BeanUtils.copyProperties(user,loginUserVo);
            loginUserVo.setToken(token);
            return ApiResult.success(loginUserVo);
        }else{
//            return ApiResult.failure(ApiCodeConstant.RESULT_LOGIN_FAIL,"用户名或密码错误");
            throw new MyException(55555);
        }
    }
}
